﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EPiServer.Templates.BPP
{
	public partial class Blog : EPiServer.SimplePage
	{
		protected override void OnPreRender(EventArgs e)
		{
			SetupControls();
			base.OnPreRender(e);
		}

		private void SetupControls()
		{
			NameLabel.Text = CurrentPage.PageName;
			InformationLiteral.Text = CurrentPage.Property["Summery"].ToString();
			PostRepeater.DataSource = EPiServer.DataFactory.Instance.GetChildren(CurrentPage.PageLink);
			PostRepeater.DataBind();
		}

		protected void PostRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			EPiServer.Core.PageData pageData = e.Item.DataItem as EPiServer.Core.PageData;
			if (pageData != null)
			{
				HyperLink postHyperLink = (HyperLink)e.Item.FindControl("PostHyperLink");
				postHyperLink.Text = pageData.PageName;
				postHyperLink.NavigateUrl = pageData.LinkURL;
				((Literal)e.Item.FindControl("CommentsLiteral")).Text = EPiServer.DataFactory.Instance.GetChildren(pageData.PageLink).Count().ToString();

				string category = pageData.Property["Category"].ToString();
				if (string.IsNullOrEmpty(category))
				{
					((Literal)e.Item.FindControl("CategoriesLiteral")).Text = "No categories";
				}
				else
				{
					((Literal)e.Item.FindControl("CategoriesLiteral")).Text = category;
				}
			}
		}
	}
}